// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import './css/terminology.css'
import './assets/jointfont/iconfont.css' // 引入joint图标
import UTIL from './util'
import CONST from './const'
import KindEditor from "./components/kindeditor/KindEditor";
import {UTable, UTableColumn} from 'umy-ui';
import axios from 'axios'
import VueAxios from "vue-axios";
import store from "./store";
import ElementUI from 'element-ui'
import qs from 'qs'
import InputDialog from './components/common/InputDialog'
import jdldUpload from './components/increaseBook/upload/js/index'

// 对于计算器, 列项, 宽度不合适;
// ElementUI.Input.props.clearable.default=true;
ElementUI.Table.props.highlightCurrentRow = {
  type: Boolean,
  default: true
}
ElementUI.TableColumn.props.showOverflowTooltip = {
  type: Boolean,
  default: true
}
// el-upload, 通过axios发送请求(便于拦截器统一处理)
// ElementUI.Upload.props.httpRequest = {
//   type: Function,
//   default: UTIL.axiosUpload
// }
Vue.use(ElementUI)
Vue.use(VueAxios, axios)
Vue.config.devtools = true
Vue.config.productionTip = false
Vue.prototype.UTIL = UTIL
Vue.prototype.CONST = CONST
window.UTIL = UTIL
window.CONST = CONST
Vue.use(jdldUpload, CONST.ENV.RESOURCE_APP_ID,CONST.ENV.RESOURCE_APP_KEY,CONST.ENV.RESOURCE_SERVER_API)
Vue.component(UTable.name, UTable);
Vue.component(UTableColumn.name, UTableColumn);

// 添加请求拦截器
axios.interceptors.request.use((config) => {
  config.headers['X-Requested-With'] = 'XMLHttpRequest'
  Pace.times += 1
  Pace.restart()
  // 解决get请求, jsonArrayStr后端接收不到问题;
  config.paramsSerializer = function(p) {
    return qs.stringify(p, {arrayFormat: 'repeat'})
  }
  // 过滤字符串null, undefined
  let params = config.method === 'post' ? config.data : config.params
  for(let key in params) {
    if (params[key] === 'null' || params[key] === 'undefined') {
      delete params[key]
    }
  }
  // URLSearchParams对象, 特殊处理;
  if (Object.prototype.toString.call(config.data) === '[object URLSearchParams]') {
    // 遍历期间不能删除数据, 先准备待删除的集合;
    let nullKeyArray = []
    config.data.forEach((val, key) => {
      if (val === 'null' || val === "undefined") {
        nullKeyArray.push(key)
      }
    })
    nullKeyArray.forEach(key => config.data.delete(key))
  }
  return config
})

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  if (Pace.times > 0) {
    Pace.times -= 1
  }
  if (Pace.times === 0) {
    Pace.stop()
  }
  if ('timeOut' === response.headers.sessionstatus) {
    window.top.location.href = response.headers.loginpage
  }
  let cacheId = UTIL.getCookie("cacheId")
  if (app.$store.state.cacheId !== cacheId) {
    app.$store.commit('refreshCacheId', cacheId)
  }
  //进入这里代表请求成功，网络请求状态码是200，但是我们要对请求的内容是否成功进行判断
  // 统一处理提示信息,查询接口不提示
  if (response.data.status === 500) {   //状态失败,全局异常信息提示
    app.$message.error(response.data.message);
    return Promise.reject(response.data);
  } else if (response.data.status === undefined) {
    return Promise.resolve(response);
  } else {
    if (response.config.url.indexOf('select') === -1) {
      app.$message.success(response.data.message);
    }
    return Promise.resolve(response.data);
  }
}, function (error) {
  if (Pace.times > 0) {
    Pace.times -= 1
  }
  if (Pace.times === 0) {
    Pace.stop()
  }
  app.$message.error("后台服务错误, 请联系管理员; " + error.message)
  return Promise.reject(error.response)
})

window.app = new Vue({
  el: '#app',
  router,
  components: {App},
  template: '<App/>',
  store,
})
// 全局注册KindEditor
Vue.component('KindEditor', KindEditor)
Vue.component('InputDialog', InputDialog)

Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                 //月份
    "d+": this.getDate(),                    //日
    "h+": this.getHours(),                   //小时
    "m+": this.getMinutes(),                 //分
    "s+": this.getSeconds(),                 //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}

Array.prototype.remove = function (val) {
  let index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
}
Array.prototype.distinct = function () {
  return Array.from(new Set(this))
}
Array.prototype.pushIfNotContains = function (item) {
  if (item && this.indexOf(item) === -1) {
    this.push(item)
  }
}
Array.prototype.unshiftIfNotContains = function (item) {
  if (item && this.indexOf(item) === -1) {
    this.unshift(item)
  }
}
Array.prototype.pushAllIfNotContains = function (items) {
  items.forEach(item => this.pushIfNotContains(item))
}
